/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic;

import java.io.IOException;

import org.elasticsearch.client.Client;
import com.google.code.morphia.DatastoreImpl;
import com.google.code.morphia.query.Query;
import com.mongodb.Mongo;
import com.morphiastic.conversion.ElasticConverter;

public class MorphiasticDataStore extends DatastoreImpl{

	
	private Morphiastic morphiastic;
	private ElasticStore elasticStore;
	
	
	public MorphiasticDataStore(Morphiastic morphiastic, Mongo mongo, ElasticStore elasticStore) {
		super(morphiastic, mongo);
		this.morphiastic = morphiastic;
		this.elasticStore = elasticStore;
//		getMapper().addInterceptor(elasticStore);
	}
	
	public MorphiasticDataStore(Morphiastic morphiastic, Mongo mongo, String dbName, String username, char[] password, ElasticStore elasticStore) {
		super(morphiastic, mongo, dbName, username, password);
		this.morphiastic = morphiastic;
		this.elasticStore = elasticStore;
//		getMapper().addInterceptor(elasticStore);
	}

	public MorphiasticDataStore(Morphiastic morphiastic, Mongo mongo, String dbName, ElasticStore elasticStore) {
		super(morphiastic, mongo, dbName);
		this.morphiastic = morphiastic;
		this.elasticStore = elasticStore;
//		getMapper().addInterceptor(elasticStore);
	}
	
	
	public void addIndexedClass(Class<?> clazz){
		//just forward to ElasticStore who does its own thing
		 this.elasticStore.addIndexedClass(clazz);
	}

	/**
	 * asks the ElasticStore to index an entity
	 * @param clazz
	 * @param entity
	 * @throws IOException 
	 */
	public void indexEntity(Class<?> clazz, Object entity){
		//just forward to the ElasticStore
		this.elasticStore.indexEntity(clazz, entity);
	}
	
	
	/**
	 * Performs the indexation part of an updateFirst operation
	 */
	public void indexUpdateFirst(Class<?> clazz, Query<?> q){
		this.elasticStore.indexUpdateFirst(clazz, q);
	}
	
	/**
	 * Performs the indexation part of an indexUpdate operation
	 * @param clazz
	 * @param q
	 */
	public void indexUpdate(Class<?> clazz, Query<?> q){
		this.elasticStore.indexUpdate(clazz, q);
	}
	
	
	
	/**
	 * deletes the entity from the index
	 * 
	 * @param clazz
	 * @param entity
	 */
	public void deleteFromIndex(Class<?> clazz, Object entity){
		this.elasticStore.deleteFromIndex(clazz, entity);
	}
	
	
	public void deleteFromIndexByQuery(Class<?> clazz, Query<?> q){
		this.elasticStore.deleteFromIndexByQuery(clazz, q);
	}
	
	
	@SuppressWarnings("rawtypes")
	public ElasticConverter getIdConverter(Class<?> clazz){
		return this.elasticStore.getIdConverter(clazz);
	}
	
	
	public String[] getEntityAssociatedTypeNames(Class<?> clazz){
		return elasticStore.getEntityAssociatedTypeNames(clazz);
	}
	
	public String getEntityTypeName(Class<?> clazz){
		return elasticStore.getEntityTypeName(clazz);
	}
	
	/**
	 * @return the elasticStore
	 */
	public ElasticStore getElasticStore() {
		return elasticStore;
	}

	/**
	 * @param elasticStore the elasticStore to set
	 */
	public void setElasticStore(ElasticStore elasticStore) {
		this.elasticStore = elasticStore;
	}
	
	public Client getElasticClient(){
		return getElasticStore().getClient();
	}
	
	
}
